package com.bulletelc.apollopre.utils;

import android.content.Context;
import android.widget.Toast;

import junit.framework.Test;

import java.util.Timer;
import java.util.TimerTask;

/**
 * author: lhl
 * time  : 2017/10/20
 * desc  : 吐司工具类
 */

public final class ToastUtil {

    private static Toast toast;

    private ToastUtil() {
        //empty
    }

    public static void showShortToast(Context context, int resId) {
        showToast(context,context.getResources().getString(resId),Toast.LENGTH_SHORT);

    }

    public static void showShortToast(Context context, CharSequence text) {
        showToast(context,text,Toast.LENGTH_SHORT);

    }

    public static void showLongToast(Context context, int resId) {
        showToast(context,context.getResources().getString(resId),Toast.LENGTH_LONG);

    }

    public static void showLongToast(Context context, CharSequence text) {
        showToast(context,text,Toast.LENGTH_LONG);

    }

    public static void showShortToast(Context context, int resId, Object... args) {
        showToast(context,context.getResources().getString(resId),Toast.LENGTH_SHORT,args);
    }

    public static void showLongToast(Context context, int resId, Object... args) {
        showToast(context,context.getResources().getString(resId),Toast.LENGTH_LONG,args);
    }

    public static void showShortToast(Context context, String text, Object... args) {
        showToast(context,text,Toast.LENGTH_SHORT,args);
    }

    public static void showLongToast(Context context, String text, Object... args) {
        showToast(context,text,Toast.LENGTH_LONG,args);
    }

    private static void showToast(Context context, String text, int duration, Object... args) {
        showToast(context,String.format(text,args),duration);
    }

    /**
     * 显示toast
     * @param context 上下文
     * @param text  显示的字符串
     * @param duration 时长, Toast.LENGTH_LONG OR  Toast.LENGTH_SHORT。
     */
    private static void showToast(Context context,CharSequence text,int duration) {
        if (toast == null) {
            toast = Toast.makeText(context, text, duration);
        } else {
            toast.setText(text);
            toast.setDuration(duration);
        }
        toast.show();

    }

    //==========================================

    /**
     * 自定义时长的吐司
     * @param context 上下文
     * @param resId 字符资源ID
     * @param cnt 时长,毫秒值
     */
    public static void showCustomDurationToast(Context context, int resId, int cnt) {
        showCustomDurationToast(context,context.getResources().getString(resId),cnt);
    }

    public static void showCustomDurationToast(Context context, int resId, int cnt, Object... args) {
        showCustomDurationToast(context,context.getResources().getString(resId),cnt,args);
    }

    public static void showCustomDurationToast(Context context, String text, int cnt, Object... args) {
        showCustomDurationToast(context,String.format(text,args),cnt);
    }


    /**
     * 自定义时长的吐司
     * @param context 上下文
     * @param msg  显示的内容
     * @param cnt  时长,毫秒值
     */
    public static void showCustomDurationToast(Context context, CharSequence msg, int cnt) {
        final Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                toast.show();
            }
        },0,3000);

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        },cnt);
    }


}
